﻿#!/usr/bin/env python
# -*- coding:utf-8 -*-

from UiManager.utils.unittesttestcase import MyUnittestTestCase
from UiManager.models import TestCaseInfo, TestStepInfo, OprationInfo, ElementInfo, LocationInfo
from UiManager.utils.resource import ElementOpration
from UiManager.utils.initialize import screen

__author__ = 'wiggins'
__all__ = ['UiUnittestTestCase']


class UiUnittestTestCase(MyUnittestTestCase):

    def test_run_method(self):
            case = self.case
            driver = self.driver
            steps = TestCaseInfo.objects.get(id=case).steps.split(',')
            self.outputs_dict['log'] = []

            for step in steps:
                self.log.info('执行步骤，step id:%s' % step)
                step_opt = TestStepInfo.objects
                step_obj = step_opt.get(id=step)
                step_must = step_obj.step_must

                element = step_obj.belong_element_id
                opration = step_obj.belong_opration_id
                input_data = step_obj.input_data

                opration_opt = OprationInfo.objects
                opration_name = opration_opt.get(id=opration).opration_name
                eo = ElementOpration(driver)

                self.log.info('步骤详情，元素:%s, 操作名称:%s, 必选操作:%s, 输入内容:%s' % (element, opration_name, step_must, input_data))
                if element:
                    belong_location_id = ElementInfo.objects.get(id=element).belong_location_id
                    location_type = LocationInfo.objects.get(id=belong_location_id).location_type
                    element_value = ElementInfo.objects.get(id=element).element_value
                    self.outputs_dict['log'].append({"location_type": location_type, "element_value": element_value})
                    if step_must == 'True':
                        self.assertEquals(eo.find_by_ui_opration(location_type=location_type,
                                                                 element_value=element_value,
                                                                 opration_name=opration_name,
                                                                 input_data=input_data), True,
                                          '找不到元素：%s, 定位方式：%s, 输入内容：%s' % (element_value, location_type, input_data))
                    else:
                        eo.find_by_ui_opration(location_type=location_type,
                                               element_value=element_value,
                                               opration_name=opration_name,
                                               input_data=input_data)

                else:
                    self.outputs_dict['log'].append({"opration_name": opration_name, "input_data": input_data})
                    eo.find_by_ui_opration(opration_name=opration_name, input_data=input_data)